<?php

namespace JLPay\Models\Marketing;

use JLPay\Models\BaseResponse;

/**
 * 营销活动数据
 */
class MarketingActivityData
{
    /**
     * 活动ID
     * @var string|null
     */
    public ?string $actId = null;

    /**
     * 活动名称
     * @var string|null
     */
    public ?string $actName = null;

    /**
     * 赞助商ID
     * @var string|null
     */
    public ?string $sponsorId = null;

    /**
     * 赞助商名称
     * @var string|null
     */
    public ?string $sponsorName = null;

    /**
     * 活动开始时间
     * @var string|null
     */
    public ?string $startTime = null;

    /**
     * 活动结束时间
     * @var string|null
     */
    public ?string $endTime = null;

    /**
     * 活动状态
     * @var string|null
     */
    public ?string $status = null;

    /**
     * 活动类型
     * @var string|null
     */
    public ?string $actType = null;

    /**
     * 创建时间
     * @var string|null
     */
    public ?string $createTime = null;

    /**
     * 更新时间
     * @var string|null
     */
    public ?string $updateTime = null;

    /**
     * 设置活动ID
     *
     * @param string $actId 活动ID
     * @return $this
     */
    public function setActId(string $actId): self
    {
        $this->actId = $actId;
        return $this;
    }

    /**
     * 设置活动名称
     *
     * @param string $actName 活动名称
     * @return $this
     */
    public function setActName(string $actName): self
    {
        $this->actName = $actName;
        return $this;
    }

    /**
     * 设置赞助商ID
     *
     * @param string $sponsorId 赞助商ID
     * @return $this
     */
    public function setSponsorId(string $sponsorId): self
    {
        $this->sponsorId = $sponsorId;
        return $this;
    }

    /**
     * 设置赞助商名称
     *
     * @param string $sponsorName 赞助商名称
     * @return $this
     */
    public function setSponsorName(string $sponsorName): self
    {
        $this->sponsorName = $sponsorName;
        return $this;
    }

    /**
     * 设置活动开始时间
     *
     * @param string $startTime 活动开始时间
     * @return $this
     */
    public function setStartTime(string $startTime): self
    {
        $this->startTime = $startTime;
        return $this;
    }

    /**
     * 设置活动结束时间
     *
     * @param string $endTime 活动结束时间
     * @return $this
     */
    public function setEndTime(string $endTime): self
    {
        $this->endTime = $endTime;
        return $this;
    }

    /**
     * 设置活动状态
     *
     * @param string $status 活动状态
     * @return $this
     */
    public function setStatus(string $status): self
    {
        $this->status = $status;
        return $this;
    }

    /**
     * 设置活动类型
     *
     * @param string $actType 活动类型
     * @return $this
     */
    public function setActType(string $actType): self
    {
        $this->actType = $actType;
        return $this;
    }

    /**
     * 设置创建时间
     *
     * @param string $createTime 创建时间
     * @return $this
     */
    public function setCreateTime(string $createTime): self
    {
        $this->createTime = $createTime;
        return $this;
    }

    /**
     * 设置更新时间
     *
     * @param string $updateTime 更新时间
     * @return $this
     */
    public function setUpdateTime(string $updateTime): self
    {
        $this->updateTime = $updateTime;
        return $this;
    }

    /**
     * 将对象转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $result = [];
        if ($this->actId !== null) $result['act_id'] = $this->actId;
        if ($this->actName !== null) $result['act_name'] = $this->actName;
        if ($this->sponsorId !== null) $result['sponsor_id'] = $this->sponsorId;
        if ($this->sponsorName !== null) $result['sponsor_name'] = $this->sponsorName;
        if ($this->startTime !== null) $result['start_time'] = $this->startTime;
        if ($this->endTime !== null) $result['end_time'] = $this->endTime;
        if ($this->status !== null) $result['status'] = $this->status;
        if ($this->actType !== null) $result['act_type'] = $this->actType;
        if ($this->createTime !== null) $result['create_time'] = $this->createTime;
        if ($this->updateTime !== null) $result['update_time'] = $this->updateTime;
        return $result;
    }
}

/**
 * 营销活动查询响应模型
 */
class MarketingActivityQueryResponse extends BaseResponse
{
    /**
     * 活动数据列表
     * @var MarketingActivityData[]
     */
    public array $data = [];

    /**
     * 构造函数
     *
     * @param array|null $responseDict 响应字典
     */
    public function __construct(?array $responseDict = null)
    {
        parent::__construct($responseDict);
        
        if ($responseDict && isset($responseDict['data']) && is_array($responseDict['data'])) {
            foreach ($responseDict['data'] as $item) {
                $dataItem = new MarketingActivityData();
                
                // 处理API返回的字段，进行字段名映射
                if (isset($item['act_id'])) $dataItem->setActId($item['act_id']);
                if (isset($item['act_name'])) $dataItem->setActName($item['act_name']);
                if (isset($item['act_status'])) $dataItem->setStatus($item['act_status']); // 映射act_status到status
                if (isset($item['start_time'])) $dataItem->setStartTime($item['start_time']);
                if (isset($item['end_time'])) $dataItem->setEndTime($item['end_time']);
                
                // 处理可能存在的其他字段
                if (isset($item['sponsor_id'])) $dataItem->setSponsorId($item['sponsor_id']);
                if (isset($item['sponsor_name'])) $dataItem->setSponsorName($item['sponsor_name']);
                if (isset($item['act_type'])) $dataItem->setActType($item['act_type']);
                if (isset($item['create_time'])) $dataItem->setCreateTime($item['create_time']);
                if (isset($item['update_time'])) $dataItem->setUpdateTime($item['update_time']);
                
                $this->data[] = $dataItem;
            }
        }
    }

    /**
     * 将响应对象转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $result = parent::toArray();
        if (!empty($this->data)) {
            $result['data'] = array_map(function($item) {
                // 安全检查：确保item是MarketingActivityData对象
                if ($item instanceof MarketingActivityData) {
                    return $item->toArray();
                } else {
                    // 如果不是对象，直接返回原始数据（这种情况不应该发生，但为了安全起见）
                    return $item;
                }
            }, $this->data);
        }
        return $result;
    }
} 